package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> byPage = setmealMapper.findByPage(setmealPageQueryDTO);

        PageResult pr = new PageResult(byPage.getTotal(),byPage.getResult());

        return pr;
    }

    @Override
    public SetmealVO findById(Long id) {
        SetmealVO byId = setmealMapper.findById(id);
        return byId;
    }

    @Override
    public int updateSetmeal(SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        int row = setmealMapper.updateSetmeal(setmeal);
        return row;
    }

    @Override
    public int updateStatus(Integer status, Long id) {
        int row = setmealMapper.updateStatus(status, id);
        return row;
    }

    @Override
    public int delectByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal byId = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == byId.getStatus()){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        int row = setmealMapper.delectByIds(ids);
        setmealMapper.deleteSetmealDish(ids);
        return row;
    }

    @Override
    @Transactional//添加事物，统一成功或失败
    public int addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());

        //调用setmealmapper层添加套餐信息
        int row = setmealMapper.addSetmeal(setmeal);
        //当前套餐的id
        Long id = setmeal.getId();
        log.info("{}"+id);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
            log.warn("{}"+setmealDish);
            //添加套餐中的菜品到套餐菜品关系表
            setmealDishMapper.addSetAndDish(setmealDish);
        }
        return 1;
    }
}
